<?php
/**
 * 此类主要处理两类应用：
 * 1. 图片压缩
 * 2. 图片选择
 * @author YcoeXu
 */
class ImageController extends FrontController {
	public function ImageController(){
		parent::FrontController();
	}
	
	public function index(){
		$file = $_GET['path'];
		$src = preg_replace('%^(.*?/?)\d+x\d+/(.*)$%', '$1$2', $file);
		$result = self::get_real_file_path($src);
		
		if($result === false){
			exit;
		}
		
		//分离出规格
		$width = preg_replace('%^.*?/?(\d+)x(\d+)/.*$%', '$1', $file);
		$height = preg_replace('%^.*?/?(\d+)x(\d+)/.*$%', '$2', $file);
		
		//原始图片路径 
		$img_path = $result['path'];
//		echo $img_path;exit;
		//压缩图片路径 
		$des_path = get_thumb_path($img_path, $width . 'x' . $height);
		
		//切割
		$handle = new ImageHandler();
		$handle->setSrcImg($img_path);
	    $handle->setDstImg($des_path);
	    $handle->createImg($width, $height);
	    if(!file_exists($des_path)){
	    	//文件创建失败
	    	exit;
	    }
// 	    header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
    	header( "Content-type: image/jpeg");
    	$size = filesize($des_path);
		$picture_data = fread(fopen($des_path, "r"), $size);
		echo $picture_data;
		exit;
	}
	
	private function get_real_file_path($url){
		$url = urldecode($url);
		//直接路径 
		$img_path = ROOT_PATH . '/' . $url;
		if(file_exists($img_path)){
			return array(
				'path' => $img_path,
				'relative_path' =>  '/' . $url
			); 
		}
		
		//私有路径 
		$img_path = ROOT_PATH . '/sites/main/' . MAIN_DOMAIN . '/template/' . $url;
		if(file_exists($img_path)){
			return array(
				'path' => $img_path,
				'relative_path' =>  '/sites/main/' . MAIN_DOMAIN . '/template/' . $url
			);
		}
		
		//公共模块路径 
		$img_path = ROOT_PATH . '/protected/web/' . MOD . MAIN_DOMAIN . '/template/' . $url;
		if(file_exists($img_path)){
			return array(
				'path' => $img_path,
				'relative_path' =>  '/sites/main/' . MAIN_DOMAIN . '/template/' . $url
			);
		}
		
		
		//公共资源路径 
		$img_path = ROOT_PATH . '/template/' . $url;
		if(file_exists($img_path)){
			return array(
				'path' => $img_path,
				'relative_path' =>  '/template/' . $url
			);
		}
		return false;
	}
	
	/**
	 * 图片选择器
	 */
	public function selector(){
		//获取当前图片分类
		$file_cate_mod = new File_cateModel();
		$image_cates = $file_cate_mod->get_cates('image');
		if(!empty($image_cates)){
			$tree = new Tree($image_cates);
			$this->assign('cates', $tree->get_formated_list('　'));
		}
		$multiple = self::get_val('multiple', 1);
		$this->assign('multiple', $multiple);
		$this->display();
	}
	
	public function file_list(){
		$file_cate_mod = new File_cateModel();
		$image_cates = $file_cate_mod->get_cates('image');
		$condition = array('site_id'=>SITE_ID, 'type'=>FileModel::get_type_by_name('image'));
		if(isset($_GET['cate'])){
			//指定了分类
			$condition['cate'] = trim($_GET['cate']);
		}
		self::init_page_info(18);
		$file_mod = new FileModel();
		$total = $file_mod->count($condition);
		self::set_page_total($total);
		if($total>0){
			$files = $file_mod->find($condition,null, $this->page_info->get_start(), $this->page_info->page_size);
			
			foreach($files as &$file){
				if(isset($image_cates[$file['cate']])){
					$file['cate_name'] = $image_cates[$file['cate']]['name'];
				}else{
					$file['cate_name'] = '未分类';
				}
			}
			self::assign('images', $files);
		}
		$this->display();
	}

	/**
	 * 为高级编辑器编写的上传文件方法
	 */
	public function upload(){
		$base_path = 'sites/main/' . MAIN_DOMAIN . '/file/' . date("Y_m") . '/';
		$result = upload_img($base_path, 'imgFile');
		header('Content-type: text/html; charset=UTF-8');
		echo json_encode($result);exit;
	}
}

?>